<?php

namespace App\Services;

use App\Models\ElectricAccountingRecords;
use App\Models\ElectricPaymentOrder;
use App\Models\ElectricPlatformAgent;

/**
 *分账操作
 * Class AliPayService
 * @package app\api\service
 */
class SplitService extends BaseService
{
    /**
     * 2021/3/20--15:10
     * @effect:转账分流操作
     * @param $data
     * @return array|null
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:splitHandle
     */
    public static function splitHandle($data): ?array
    {
        if (!is_array($data)) {
            $data = json_decode($data, 1);
        }

        $order_fields = 'id,fatherId,orderNo,pay_type,pay_no,split_money,paymentMoney,after_handling,after_split,handlingFee,user_id';
        $order = ElectricPaymentOrder::selectRaw($order_fields)
            ->where(['orderNo' => $data['orderNo']])
            ->first();//根据订单号查询订单
        if ($order) {
            $order = $order->toArray();
        } else {
            $order = [];
        }
        if (isset($order['split_money']) && $order['split_money'] > 0) {
            $agent_fields = 'wechat_account_type,wechat_account,sub_mch_id,id,alipay_account,alipay_account_type';
            $agent = ElectricPlatformAgent::selectRaw($agent_fields)
                ->find($order['fatherId']);
            switch ($data['pay_type']) {
                case 1:
                case 2:
                    $result = (new WePaySplitService)->WePaySplitDo(['one_order' => $order, 'agent' => $agent]);//去往具体分账操作
            dd($result);
                    $b = ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') || $result['err_code_des'] === '分账金额不足';
                    if ($b) {
                        $bool = WePaySplitService::afterSplit($data, $order, $agent);
                        $str = "分账成功 分账金额：{$order['split_money']}；提现手续费：{$order['handlingFee']}；分账结余：{$order['after_split']}；订单号：{$order['orderNo']}";
                        return ['code' => 200, 'msg' => $str];
                    }

                    $str = "分账失败 状态码：{$result['err_code']}；错误描述： {$result['err_code_des']}；订单号：{$order['orderNo']}";
                    return ['code' => 400, 'msg' => $str];
                    break;
                case 3:

                    break;
            }
        } else {
            return ['code' => 400, 'msg' => '分账金额不正确'];
        }
    }
    /**
     * 2021/3/20--13:35
     * @effect:插入分账记录
     * @param $fatherId
     * @param $split_money
     * @param $owner
     * @param $pay_type
     * @param $orderNo
     * @param $handlingFee
     * @param $after_split
     * @param $user_id
     * @param $split_type
     * @return mixed
     *@author:苏冬冬
     * @QQ:1299273928
     * @method:insert_split_log
     */public static function insert_split_log($fatherId, $split_money, $owner, $pay_type, $orderNo, $handlingFee, $after_split, $user_id, $split_type)
{
    $data2 = [
        'agent_id' => $fatherId,//代理商id
        'money' => $split_money,//分账金额
        'number' => $owner,//房间号
        'type' => $pay_type,//缴费类型
        'orderNo' => $orderNo,
        'handlingFee' => $handlingFee,//手续费
        'afterSplit' => $after_split,//分账后余额
        'from_user' => $user_id,
        'split_type' => $split_type,
    ];
    return ElectricAccountingRecords::create($data2);
}

}
